package com.y.comtoo.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.LinkedList;

import com.y.comtoo.utils.StrUtil;

import lombok.Getter;

public class FileTool {
	
	/** 替换规则 */
	enum RepRule{
		/**替换原行*/
		Replace, 
		/** 保留原行文本，并在原行文本上面插入新行 */
		Insert,
		/** 保留原行文本，并在原行文本下面追加新行 */ 
		Append;
	}
	
	@Getter
	class RepRuleVo{
		private String matchStr;
		private LinkedList<String> replaceStr = new LinkedList<String>();
		private RepRule rule = RepRule.Replace;

		public RepRuleVo(String matchStr) {
			super();
			this.matchStr = matchStr;
		}
		
		public RepRuleVo(String matchStr, String replaceStr) {
			super();
			this.matchStr = matchStr;
			this.replaceStr.add(replaceStr);
		}
		public RepRuleVo(String matchStr, LinkedList<String> replaceStr) {
			super();
			this.matchStr = matchStr;
			this.replaceStr.addAll(replaceStr);
		}
		
		public RepRuleVo(String matchStr, LinkedList<String> replaceStr, RepRule rule) {
			super();
			this.matchStr = matchStr;
			this.replaceStr = replaceStr;
			this.rule = rule;
		}
	}
	
	public static void main(String [] args) throws IOException {
		FileTool.Copy("D:\\aaa\\AF03IssuesList.java", "D:\\aaa\\AF03IssuesList.java");
	}
	
	/**复制一个文件
	 * @param replaceRule 替换规则， 前面是表达式，后面是要替换的语句表达式  
	 * @throws IOException */
	public static boolean Copy(String oldFullName, String newFullName) throws IOException {
		if(StrUtil.IsBlankIncNullStrs(oldFullName, newFullName)) {
			return false;	
		}
		File f = new File(oldFullName);
		if(!f.isFile() || !f.exists()) {
			return false;
		}
		/* 要写入新文件的每行数据 */
		LinkedList<String> newLins = new LinkedList<String>();
		
		Files.lines(Paths.get(oldFullName)).forEach(str->{
			System.out.println(str);
			if(StrUtil.isBlank(str)) {
				newLins.add("");
			}else if("import javax.persistence.Version;".equals(str)) {
				System.out.println("替换乐观锁导入");
				if(!newLins.contains("import com.baomidou.mybatisplus.annotation.Version;")) {
					newLins.add("import com.baomidou.mybatisplus.annotation.Version;");
				}
			}else if("import javax.persistence.Transient;".equals(str)) {
				System.out.println("替换非数据库字段导入");
				if(!newLins.contains("import com.baomidou.mybatisplus.annotation.TableField;")) {
					newLins.add("import com.baomidou.mybatisplus.annotation.TableField;");
				}
			}else if("import javax.persistence.Table;".equals(str)) {
				System.out.println("替换 Table 导入");
				if(!newLins.contains("import com.tangzc.mpe.autotable.annotation.Table;")) {
					newLins.add("import com.tangzc.mpe.autotable.annotation.Table;");
				}
				if(!newLins.contains("import com.baomidou.mybatisplus.annotation.TableName;")) {
					newLins.add("import com.baomidou.mybatisplus.annotation.TableName;");
				}
			}else if("import javax.persistence.Entity;".equals(str)) {
				System.out.println("替换 Entity 导入");
				if(!newLins.contains("import com.tangzc.mpe.automapper.AutoMapper;")) {
					newLins.add("import com.tangzc.mpe.automapper.AutoMapper;");
				}
			}else if("import javax.persistence.Id;".equals(str)) {
				System.out.println("替换ID导入");
				if(!newLins.contains("import com.tangzc.mpe.autotable.annotation.ColumnId;")) {
					newLins.add("import com.tangzc.mpe.autotable.annotation.ColumnId;");
				}
			}else if("import javax.persistence.Column;".equals(str)) {
				System.out.println("替换Column导入");
				if(!newLins.contains("import com.tangzc.mpe.autotable.annotation.Column;")) {
					newLins.add("import com.tangzc.mpe.autotable.annotation.Column;");
				}
				if(!newLins.contains("import com.baomidou.mybatisplus.annotation.TableField;")) {
					newLins.add("import com.baomidou.mybatisplus.annotation.TableField;");
				}
				if(!newLins.contains("import com.baomidou.mybatisplus.annotation.FieldStrategy;")) {
					newLins.add("import com.baomidou.mybatisplus.annotation.FieldStrategy;");
				}
			}else if("import javax.persistence.GeneratedValue;".equals(str)) {
				System.out.println("删除 GeneratedValue 导入");
			}else if("import javax.persistence.MappedSuperclass;".equals(str)) {
				System.out.println("删除 GeneratedValue 导入");
			}else if("@MappedSuperclass".equals(str)) {
				System.out.println("删除 @MappedSuperclass 注解 ");
			}else if(str.contains("@GenericGenerator")) {
				System.out.println("删除 @GenericGenerator 注解 ");
			}else if("import org.hibernate.annotations.GenericGenerator;".equals(str)) {
				System.out.println("删除 @import org.hibernate.annotations.GenericGenerator; 导入 ");
			}else if(str.contains("@Type(type=\"text\")")) {
				System.out.println("删除 @Type(type=\"text\") 注解 ");
			}else if(str.contains("@GeneratedValue")) {
				System.out.println("删除 @GeneratedValue 注解 ");
			}else if(str.contains("@Id")) {
				newLins.add("@ColumnId(value = \"id\", length = 64, comment = \"数据库主键\", mode = IdType.ASSIGN_UUID)");
			}else if("@Entity".equals(str)) {
				System.out.println("替换 @Entity 注解 ");
				if(!newLins.contains("@AutoMapper")) {
					newLins.add("@AutoMapper");
				}
				if(!newLins.contains("@TableName(autoResultMap = true)")) {
					newLins.add("@TableName(autoResultMap = true)");
				}
			}else if(str.contains("@Table(") || str.contains("@Table (")) {
				System.out.println("替换 @Table(name=  注解 ");
				String s = str.replace("name =", "value=").replace("name=", "value="); 
				newLins.add(s);
			}else if(str.contains("@Transient")) {
				System.out.println("替换 @Transient 注解 ");
				newLins.add(str.replace("@Transient","@TableField(exist = false)"));
			}else if(str.contains("@Column(") || str.contains("@Column (") || str.contains("@Column(")) { 
				if(str.contains("\"id\"")) {
					System.out.println("删除 id 字段列声明 ");
				}else {
					System.out.println(" 替换 @Column 注解中的属性 ");
					String s = str.replace("name =", "value=").replace("name=", "value="); 
					if(str.contains("columnDefinition=\"CLOB\"")) {
						s = s.replace("@Column", "@TableField").replace("columnDefinition=\"CLOB\"", "typeHandler = org.apache.ibatis.type.ClobTypeHandler.class"); 
					}else {
						s = s.replace("nullable= false", "notNull=true").replace("nullable=false", "notNull=true").replace("nullable = false", "notNull=true");
						s = s.replace(", updatable=false)", ")").replace(", updatable = false)", ")").replace("updatable=false,", "").replace("updatable = false,", "").replace("updatable=false", "").replace("updatable = false", "");
						s = s.replace("scale =", "decimalLength=").replace("scale = ", "decimalLength=").replace("scale=", "decimalLength=");
						s = s.replace("precision =", "length=").replace("precision=", "length=").replace("precision = ", "length=");
						if(str.contains("updatable=")) {
							newLins.add("	@TableField(updateStrategy = FieldStrategy.NEVER)");
						}
					}
										
					newLins.add(s);
				}
			}else {
				newLins.add(str);
			}
			
		}); 
	
		Files.write(Paths.get(newFullName), newLins, StandardOpenOption.WRITE);

		return true;
	}
}
